package day16.FanSe.FS02;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Text02 {
//    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
////        method1();
////        method2();
////        method3();
//        method4();


    private static void method4() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> aClass = Class.forName("day16.FanSe.FS02.Student");
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class);
//        被private修饰的成员,不能直接使用,如果用反射强行获取并使用,需要取消访问权限
        constructor.setAccessible(true);
        Student student =(Student) constructor.newInstance("zhangsan");
        System.out.println(student);
    }

    private static void method3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //        Student...的无参构造方法
        Class<?> aClass = Class.forName("day16.FanSe.FS02.Student");
        Student student =(Student) aClass.newInstance();
        System.out.println(student);
    }

    private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//        Student...的无参构造方法
        Class<?> aClass = Class.forName("day16.FanSe.FS02.Student");
        Constructor<?> constructor = aClass.getConstructor();
        Student student = (Student)constructor.newInstance();
        System.out.println(student);
    }

    private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> aClass = Class.forName("day16.FanSe.FS02.Student");
        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
        Student student =(Student) constructor.newInstance("zhangsan", 23);
        System.out.println(student);
    }
}
